Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- gcc -std=c99 -Wall -o "input" "input.c"
- ./input < test.txt
- test.txt
- 3
- Петр
- Иванов
- 14
- Иван
- Петров
- 34
- Алена
- Смирнова
- 44
- */
- #include <stdio.h>
- #include <stdlib.h>
- struct stud{
- char name[50];
- char fname[50];
- int balls;
- };
- typedef struct stud st;
- void readStudent(st* student){
- printf("Введите Имя:");
- scanf("%s", student->name);
- printf("Введите Фамилию:");
- scanf("%s", student->fname);
- printf("Введите баллы:");
- scanf("%d", &student->balls);
- }
- void readStudents(st** student, int count){
- for (int i = 0; i < count ; i++){
- student[i] = (st*) malloc (sizeof(st));
- readStudent(student[i]);
- }
- }
- void printStudents(st** student, int count){
- for (int i = 0; i < count ; i++){
- printf("Имя:");
- printf("%s\n", student[i]->name);
- printf("Фамилия:");
- printf("%s\n", student[i]->fname);
- printf("баллы:");
- printf("%d\n", student[i]->balls);
- }
- }
- static int cmp(const void *p1, const void *p2){
- st * st1 = *(st**)p1;
- st * st2 = *(st**)p2;
- return st2->balls - st1->balls;
- }
- int main(int argc, char **argv){
- int count = 3;
- printf("Введите кол-во:");
- scanf("%d", &count);
- st** mas = (st**)malloc(sizeof(st**)*count);
- readStudents(mas, count);
- qsort(mas, count, sizeof(st*), cmp);
- printStudents(mas, count);
- for (int i = 0; i < count; i++)
- {
- free(mas[i]);
- }
- free(mas);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement